package di.er.zhong.Test;

import di.er.zhong.Model.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaolmpl {
    private QueryRunner queryRunner = new QueryRunner();
//
//  1.查询所有的数据
    public List<User> selectAll() {

        try {
            List<User> user = queryRunner.query(DBUtils.getConnection(),
                    "select * from user;",
                    new BeanListHandler<User>(User.class));
            return user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    /*
     * 2.实现删除的业务逻辑接口
     * */
    public int delete(int id) {
        try {
            int result = queryRunner.update(DBUtils.getConnection(),
                    "delete from user where id=?;", id);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /*
     * 3.更新的业务逻辑
     * */
    public int update(User user) {
        try {
//            int result =queryRunner.update(DbUtils.getConnection(),
//                    "update empset name=?,salary=?,age=? where id=?",
//                    emp.getName(),emp.getSalary(), emp.getAge(), emp.getId());
            int result = queryRunner.update(DBUtils.getConnection(),
                    "update user set name=?,password=?,nickname=? where id=?;",
                    user.getName(),user.getPassword(), user.getNickname(), user.getId());

            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /*
     * 选择一条数据
     * */

    public User select(int id) {
        System.out.println("数据操作层：id = " + id);
        try {
//            Emp emp =queryRunner.query(DbUtils.getConnection(),
//                    "select * fromemp where id=?;",
//                    newBeanHandler<Emp>(Emp.class),id);

            User user = queryRunner.query(DBUtils.getConnection(),
                    "select * from user where id=?;",
                    new BeanHandler<User>(User.class), id);

            return user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /*
     * 插入一条数据
     * */

    public int insert(User user) {
        System.out.println("数据操作的DAO：user.toString() = " + user.toString());
        try {
//            int result =queryRunner.update(DbUtils.getConnection(),
//                    "update empset name=?,salary=?,age=? where id=?",
//                    emp.getName(),emp.getSalary(), emp.getAge(), emp.getId());
            int result = queryRunner.update(DBUtils.getConnection(),
                    "insert into user(id,name,password,nickname) values(?,?,?,?);",
                    user.getId(),user.getName(),user.getPassword(),user.getNickname());
            System.out.println("数据入库成功啦--result =" + result);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
